﻿using DataChangeTools.AutoUpdater;
using DataChangeTools.AutoUpdater.Views;
using DataChangeTools.Events;
using DataChangeTools.Extensions;
using DataChangeTools.Utils;
using HandyControl.Data;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace DataChangeTools.ViewModel
{
    public class MainViewModel : BindableBase
    {
        private readonly IRegionManager regionManager;
        private readonly IDialogService _dialogService;
        private readonly IEventAggregator _eventAggregator;
        public MainViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
        {
            OpenCommand = new DelegateCommand<string>(Open);
            AutoUploadCmd = new DelegateCommand<System.Windows.Controls.Button>(AutoUpload);
            this.regionManager = regionManager;
            _dialogService = dialogService;
            _eventAggregator = eventAggregator;
            InintData();
        }


        #region 字段
        private string configVersion;

        public string ConfigVersion
        {
            get { return configVersion; }
            set { configVersion = value; RaisePropertyChanged(); }
        }

        private string baseVersion;

        public string BaseVersion
        {
            get { return baseVersion; }
            set { baseVersion = value; RaisePropertyChanged(); }
        }

        #endregion



        //负责执行不同的模块
        public DelegateCommand<string> OpenCommand { get; private set; }
        public DelegateCommand<System.Windows.Controls.Button> AutoUploadCmd { get; private set; }

        private object body;
        public object Body
        {
            get { return body; }
            set { body = value; RaisePropertyChanged(); }
        }
        private void Open(string obj)
        {
            var NameSpace = "";
            if (obj == null || string.IsNullOrWhiteSpace(obj))
                return;

            regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(obj, back =>
            {

            });
        }


        private void InintData()
        {
            string version1 = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            //MessageBox.Show($"应用程序版本: {version1}");

            //string version = Assembly.GetEntryAssembly()?.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
            //.OfType<AssemblyFileVersionAttribute>().FirstOrDefault()?.Version;

            ConfigVersion = version1;
            BaseVersion = version1;

            _eventAggregator.GetEvent<GrowlEvent>().Subscribe((GrowMsg msg) =>
            {
                if (msg.enumAlarmType == EnumAlarmType.Info)
                {
                    HandyControl.Controls.Growl.Info(new GrowlInfo()
                    {
                        Message = msg.message,
                        WaitTime = 2,
                        IsCustom = true
                    });
                }
                else if (msg.enumAlarmType == EnumAlarmType.Info)
                {
                    HandyControl.Controls.Growl.Warning(new GrowlInfo()
                    {
                        Message = msg.message,
                        WaitTime = 2,
                        IsCustom = true
                    });
                }
                else if (msg.enumAlarmType == EnumAlarmType.Err)
                {
                    HandyControl.Controls.Growl.Error(new GrowlInfo()
                    {
                        Message = msg.message,
                        WaitTime = 2,
                        IsCustom = true
                    });
                }
                else if (msg.enumAlarmType == EnumAlarmType.Fatal)
                {
                    HandyControl.Controls.Growl.Fatal(new GrowlInfo()
                    {
                        Message = msg.message,
                        WaitTime = 2,
                        IsCustom = true
                    });
                }
                else
                {
                    HandyControl.Controls.Growl.Info(new GrowlInfo()
                    {
                        Message = msg.message,
                        WaitTime = 2,
                        IsCustom = true
                    });
                }
            });


        }

        private void AutoUpload(System.Windows.Controls.Button obj)
        {
            // 显示弹窗

            _dialogService.ShowDialog("AutoUpdaterView", null, result =>
            {
                // 弹窗关闭后的回调
                if (result.Result == ButtonResult.OK)
                {
                    // 处理OK按钮的逻辑

                }
                else if (result.Result == ButtonResult.Cancel)
                {
                    // 处理Cancel按钮的逻辑
                }
            });
        }

        public void PostGrowlEvent(string strMessage, EnumAlarmType enumat)
        {
            _eventAggregator.GetEvent<GrowlEvent>().Publish(new GrowMsg()
            {
                enumAlarmType = enumat,
                message = strMessage,
            });
        }

    }


}
